package Domain.forumManager;

import java.util.Vector;
import java.util.logging.Level;


import DB.DataBase;
import Domain.stompProtocol.ForumFrame;
import Logger.ForumLogger;
import objects.Frm;
import objects.Message;
import objects.Trd;
import objects.User;

public class ForumsManager
{
	private DataBase _database;
	public ForumsManager(DataBase database)
	{
		this._database = database;
	}

	/**
	 * 
	 * @param nameOfForum
	 * @param nameOfUser
	 * @return -1 if can't find user , -5 if user is a default user  , 1 if forum added
	 */
	public int addForum (String nameOfForum,String nameOfUser)
	{
		User u = this._database.findUser(nameOfUser);
		if (u == null)
		{
			//System.out.println("can't find user "+nameOfUser);
			return ForumFrame.ERRUserNotFound;
		}

		else {


			if(u.getType()!=User.Type.MANAGER)
			{
				return ForumFrame.ERRInvalidTypeOfUSer;
			}else
			{
				if (this._database.addForum(nameOfForum)== null)
					return ForumFrame.ForumNameExist;	
				else return 1;
			}
		}

	}

	/**
	 * 
	 * @param nameOfForum
	 * @param nameOfUser
	 * @param nameOfThread
	 * @return -1 if user not found ,-2 if user doesn't a manager ,-3 if forum doesn't exist
	 * -4 if thread could not be added (probably because title already exist in forum) 
	 * 1 if thread added
	 */
	public int addThread (String nameOfForum,String nameOfUser,String nameOfThread)
	{
		User u=this._database.findUser(nameOfUser);
		if (u==null)
		{
			//System.out.println("can't find user "+nameOfUser);
			return -1;
		}


		if(u.getType()!=User.Type.MANAGER)
		{
			ForumLogger.write(Level.SEVERE,"the Default user: "+u.getUserName()+" tried to add thread");
			return -2;

		}else
		{
			if (this._database.findFroum(nameOfForum)== null)
			{
				//System.out.println("the forum "+ nameOfForum+" doesn't exist");
				return -3;
			}
			if (this._database.addThreadToFroum(nameOfForum, nameOfThread) == null)
			{
				//System.out.println("could not add thread to forum "+nameOfForum+
				//" ,title of thread must be unique in forum.");
				return ForumFrame.ThreadNameExist;//-10

			}
		}
		return 1;

	}




	/**
	 * how is use this function  need to send the announsment to all of the User Friends
	 * @param nameOfForum the name of the forum to add the message
	 * @param nameOfUser the name of the User that added the message
	 * @param nameOfThread the name of the thread to add the message
	 * @param msg the msg to be added
	 * @return the vector of the friend to send them the message
	 */
	public Vector <User> addMessage (String nameOfForum,String nameOfUser,String nameOfThread,String title,String msg)
	{
		User u=this._database.findUser(nameOfUser);
		if (u == null)
		{
			//System.out.println("can't find user "+nameOfUser);
			return null;
		}


		Frm frm = this._database.findFroum(nameOfForum);
		Trd thread = this._database.getThread(frm, nameOfThread);
		try{
			_database.addMessage(thread,title, u, msg);
		}catch (org.hibernate.exception.ConstraintViolationException e) {
			return null;
		}
		return _database.getFriendsOfUser(nameOfUser);


	}
	/**
	 * 
	 * @param nameOfUser
	 * @param nameOfForum
	 * @param nameOfThread
	 * @param titleOfMessage
	 * @param newcontent
	 * @return -2 if thread doesn't found , return -1 if msg doesn't found , -3 if user doesn't found
	 *  -4 if forum doesn't found ,-5 unkown problem,return ,-6 if user not owner or manager,1 on seccss  
	 */
	public int editMessage (String nameOfUser, String nameOfForum , String nameOfThread, String titleOfMessage,String newContent)
	{
		User u = this._database.findUser(nameOfUser);
		if(u==null)
		{
			return -3;
		}
		try{
			Frm f = this._database.findFroum(nameOfForum);
			if (f==null)
				return -4;

			return _database.editMessageInThread(f,u,nameOfThread, titleOfMessage, newContent);
		}
		catch(NullPointerException e)
		{
			ForumLogger.write(Level.INFO,"got Null in edit message");
			return -5;
		}


	}


	/**
	 * 
	 * @param nameOfForum
	 * @param nameOfUser
	 * @param nameOfThread
	 * @param title
	 * @return -5 if user not owner nor manager,-4 if fail to find user, /
	 * -3 if can't find forum ,-2 if thread doesn't found ,/ 
	 *return -1 if message doesn't found ,/
	 * return 1 if object remove
	 */
	public int removeMessage(String nameOfForum,String nameOfUser,String nameOfThread,String title)
	{
		User user = this._database.findUser(nameOfUser);
		if(user == null)
		{
			//System.err.println("can't find user "+nameOfUser);
			return ForumFrame.ERRUserNotFound;
		}
		User messageOwner = this._database.findOwnerOfMessage(nameOfForum, nameOfThread, title);
		if (messageOwner==null)
		{
			//System.err.println("can't find owner");
			return ForumFrame.UNKOWNERR;
		}
		//System.out.println("WE ARE HERE before if-----------------");
		if (messageOwner.getUserName().equals(user.getUserName())|| user.getType() == User.Type.MANAGER)
		{
			//System.out.println("WE ARE HERE in if -----------------");
			Frm forum = this._database.findFroum(nameOfForum);
			if (forum == null)
				return ForumFrame.ERRForumNotFound;
			return _database.removeMessageFromThread(forum,nameOfThread, title);

		}
		else return ForumFrame.ERRInvalidTypeOfUSer;

	}


	public Vector<Trd> getThreads(String nameOfForum)
	{
		return this._database.getThreads(this._database.findFroum(nameOfForum));
	}
	
	public Vector<Message> getMessages(String nameOfForum, String nameOfThread)
	{
		return this._database.getMessageFromThread(nameOfForum, nameOfThread);
	}


	public Vector<Frm> getForums ()
	{
		return this._database.getForums();
	}



}
